﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Web.Controllers
{
    public static class Convertor
    {
        public static bool ToBool(this string str)
        {
            if (str == null) return false;
            str = str.ToLower();
            return (str == "on" || str == "true");
        }

        public static bool? ToSex(this int sex)
        {
            if (sex == 1) return true;
            else if (sex == 2) return false;
            else return null;
        }

        public static string ToQ(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return null;
            else
                return str;
        }

        public static string ToHtmlQ(this string str)
        {
            if (string.IsNullOrWhiteSpace(Core.functional.stripHtml(str)))
                return null;
            else
                return str;
        }

        public static string TrimHtmlEnd(this string str)
        {
            if (string.IsNullOrWhiteSpace(str)) return "";
            return Regex.Replace(str, @"(&nbsp;|<br\s*/?>|\s)+$", "", RegexOptions.IgnoreCase);
        }
    }
}